﻿using lwzlz.Common;
using lwzlz.Web.Areas.SysAdmin.Models;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    public class FileController : AdminController
    {
        /// <summary>
        /// 上传头像
        /// </summary>
        /// <returns></returns>
        public JsonResult UpLoadUserHeadPic()
        {
            return Json(UpLoad($"/upload/userface/"));
        }

        /// <summary>
        /// 上传商品
        /// </summary>
        /// <returns></returns>
        public JsonResult UpLoadCommondityPic()
        {
            return Json(UpLoad($"/upload/commondity/"));
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public FileMsg UpLoad(string path)
        {
            FileMsg msg = new FileMsg();
            try
            {
                msg.data = new List<FileList>();
                HttpPostedFileBase file = Request.Files[0];
                string fileName = ZFiles.UpFile(file, path);
                msg.data.Add(new FileList { src = fileName });
                msg.code = 0;
                msg.msg = "成功";
            }
            catch (Exception ee)
            {
                msg.code = 1;
                msg.msg = "失败：" + ee.Message.ToString();
            }
            return msg;
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public FileMsg UpLoad(string path, HttpPostedFileBase file)
        {
            FileMsg msg = new FileMsg();
            try
            {
                msg.data = new List<FileList>();
                string fileName = ZFiles.UpFile(file, path);
                msg.data.Add(new FileList { src = fileName });
                msg.code = 0;
                msg.msg = "成功";
            }
            catch (Exception ee)
            {
                msg.code = 1;
                msg.msg = "失败：" + ee.Message.ToString();
            }
            return msg;
        }
    }
}